const JwtStrategy = require('passport-jwt').Strategy,
    ExtractJwt = require('passport-jwt').ExtractJwt;
const User = require('../model/user')
const secretOrKey = require("../config/keys");
const opts = {}
opts.jwtFromRequest = ExtractJwt.fromAuthHeaderAsBearerToken(); //通过配置信息来生成jwt的请求，验证这个token
opts.secretOrKey = secretOrKey;

module.exports = passport => {
    passport.use(new JwtStrategy(opts, async(jwt_payload, done) => {
        // console.log(jwt_payload);
        try {
            const user = await User.findById(jwt_payload.id)
            if (user) {
                return done(null, user)
            }
            return done(null, false)
        } catch (err) {
            console.log(err);
        }
    }));
}